﻿@model List<deviceManage.Models.DocumentType>

@{
    Layout = null;
}

<div class="layui-form" lay-filter="uploadForm" style="padding: 20px 30px 0 0;">    
        <div class="layui-form-item">
            <label class="layui-form-label">文档类型</label>
            <div class="layui-input-inline">
                <select name="typeId" lay-verify="required" >
                
                    @foreach(var type in @Model)
                {
                    <option value="@type.Id">@type.Name</option>
                }
                </select>
             </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-card">
                <div class="layui-card-header">                    
                    <button type="button" class="layui-btn layui-btn-normal layui-btn-xs" id="uploadBtn" style="margin-left:20px;">
                        <i class="layui-icon layui-icon-upload-drag"></i>添加文件
                    </button>
                </div>
                <div class="layui-card-body">
                    
                        <table class="layui-table" id="docTable" lay-filter="docTable">
                            <thead>
                                <tr>
                                    <th lay-data="{field:'id',  sort: true}">文档名称</th>                                    
                                    <th lay-data="{field:'opt',width:120, sort: true}">操作</th>
                                </tr>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>
                    
                </div>
            </div>
        </div>
        <div class="layui-form-item center">
            <input type="button" lay-submit="" lay-filter="save" value="确认" class="layui-btn">
        </div>
    </div>
<script type="text/javascript">
    form.render("", 'uploadForm');


    //上传照片
    upload.render({
        elem: "#uploadBtn" //绑定元素
        , url: '/doc/uploadTemp'//上传接口        
        , accept: 'file'
        , done: function (res) {
            layer.msg(res.msg);
            if (res.code == 1) {   
                var doc = res.data[0];
                var html = "<tr class='doc_tr' data-id='"+doc.id+"' id='tr_"+doc.id+"'><td>" + doc.name + "</td><td> <a class=\"layui-btn layui-btn-danger layui-btn-xs\" onclick=\"deleteItem('" + doc.id +"')\"><i class=\"layui-icon layui-icon-delete\"></i>删除</a> </td></tr>";
                $('#docTable').find('tbody').append(html);
                //转换静态表格
                initUploadTable();
            }
        }
        , error: function () {
            layer.msg("上传失败！");
        }
    });

    //删除文档
    function deleteItem(id) {
        layer.confirm("确定要删除吗？", function (index) {
            layer.close(index);
            $.post("/doc/delete?id=" + id,
                function (res) {
                    layer.msg(res.msg);
                    if (res.code == 1) {
                        $("#tr_" + id).remove();
                        initUploadTable();
                    }
                });
        });
    }

    function initUploadTable() {
        table.init('docTable', {
            height: 220 //设置高度
            , limit: 100
        });
    }

    //监听提交
    form.on('submit(save)', function (data) {
        var field = data.field; //获取提交的字段        
        var typeId = field.typeId;
        var list = [];
        $(".doc_tr").each(function () {
            var id = $(this).data("id");
            if (id != '') list.push(id);
        });
        $.post("/doc/BatchSaveUploadFile",
            { typeId: typeId,list:list }
            , function (d) { layer.msg(d.msg); if (d.code == 1) { tableObj.reload(); layer.close(editWin) } });

    });
</script>